//
//  BiXingDanXuanTiViewController.swift
//  CKC
//
//  Created by 张振东 on 2016/10/20.
//  Copyright © 2016年 ZZD. All rights reserved.
//

import UIKit
import RealmSwift

class BiXingDanXuanTiViewController: UIViewController {

    let screenWidth = UIScreen.main.bounds.width
    let screenHeight = UIScreen.main.bounds.height
    var navigationHeight:CGFloat?
    
    //定义一个变量:记录题号的值,默认为1
    var tiHao:Int = 1
    
    //定义一个变量:记录产生的随机数的值
    var randomNumber:UInt32?
    
    var randomNumberValue:Int?
    var leftTopImageView:UIImageView?
    var rightTopImageView:UIImageView?
    var leftDownImageView:UIImageView?
    var rightDownImageView:UIImageView?
    
    //定义四个lable对象,记录A、B、C、D
    var buttonA:UIButton?
    var buttonB:UIButton?
    var buttonC:UIButton?
    var buttonD:UIButton?
    
    //定义三个label,用于记录题干的内容
    var tiHaoLabel:UILabel?
    
    var tiGanNeiRongLabel:UILabel?
    
    var randomNumberLable:UILabel?
    
    var diShiLabel:UILabel?
    
    var zuoKuoHaoLabel:UILabel?
    var selectedOptionLabel:UILabel?
    var youKuoHaoLabel:UILabel?
    
    
    //定义两个按钮:保存,下一题
    var saveBtn:UIButton?
    var nextQuestionBtn:UIButton?
    
    //定义一个变量,用于存储从文件夹中读取的图片资源列表
    var imageList:[String]?
    
    //定义两个数组,分别存储随机数所在的数组,剩余的图片的名称
    var randomNumberArray = [String]()
    var otherNumberArray = [String]()
    
    //定义一个变量,用于保存随机生成的图片的名称
    var leftTopImageName:String?
    var rightTopImageName:String?
    var leftDownImageName:String?
    var rightDownImageName:String?
    
    //定义一个变量,用于记录正确答案的位置
    var suiJiShuBetweenLingHeSan:Int = 0
    
    override func viewDidLoad() {
        
        fengZhuangKongJianShuXing()
        fengZhuangViewDidLoad()
        
        //弹出一个提示框,显示没有收藏题了
        let alertController = UIAlertController(title: "温馨提示",
                                                message: "亲,您共有20道热身题哦,如果您选择正确,按钮颜色将变为绿色,如果错误,按钮将变为灰色!!", preferredStyle: .alert)
        let okAction = UIAlertAction(title: "好的", style: .default, handler: {
            action in
            print("点击了确定")
        })
        alertController.addAction(okAction)
        self.present(alertController, animated: true, completion: nil)

        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    /*
        作者:张振东
        日期:2017-10-20
        功能:封装lable属性
     */
    func fengZhuangLabelAttributes(_ label:UILabel){
        label.font = UIFont(name: "FontAwesome",size: 15)
        switch label {
        case tiHaoLabel!:
            label.text = "\(tiHao)"
            label.font = UIFont(name: "FontAwesome",size: 20)
            break
        case tiGanNeiRongLabel!:
            label.text = "、以下笔形对应的笔形码为"
            break
        case randomNumberLable!:
            label.font = UIFont(name: "FontAwesome",size: 17)
            
            if let randomNumber = randomNumber{
                label.text = "\(randomNumber)"
                label.textColor = UIColor.red
            }
            break
        case diShiLabel!:
            label.text = "的是:"
            break
        case zuoKuoHaoLabel!:
            label.text = "("
            break
        case selectedOptionLabel!:
            label.text = ""
            break
        case youKuoHaoLabel!:
            youKuoHaoLabel?.text = ")"
            break
        default:
            print("封装完毕")
        }
    }
    
    /*
     作者:张振东
     日期:2017-10-20
     功能:封装button属性
     */
    func fengZhuangButtonAttributes(_ button:UIButton){
//        button.setTitleColor(UIColor.blackColor(), forState: .Normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        button.backgroundColor = UIColor(red: 231/255,green: 76/255,blue: 60/255,alpha: 1)
        button.layer.cornerRadius = 5
        switch button {
        case buttonA!:
             button.setTitle("A", for: UIControlState())
             break
        case buttonB!:
            button.setTitle("B", for: UIControlState())
            break
        case buttonC!:
            button.setTitle("C", for: UIControlState())
            break
        case buttonD!:
            button.setTitle("D", for: UIControlState())
            break
        case saveBtn!:
            button.setTitle("收藏", for: UIControlState())
            break
        case nextQuestionBtn!:
            button.setTitle("下一个", for: UIControlState())
            break
        default:
            print("封装button")
        }
    }
    
    /*
     作者:张振东
     日期:2017-10-20
     功能:随机生成一个0-10之间的数
     */
    func suiJiShengChengYiGeShu(){
        randomNumber = arc4random_uniform(UInt32(10))
        randomNumberValue = Int(randomNumber!)
    }
    
    func fengZhuangKongJianShuXing(){
        self.title = "单选题热身"
        self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white,NSFontAttributeName:UIFont(name: "STHeitiSC-Medium", size: 20.0)!]
        
        self.view.backgroundColor = UIColor(red: 246,green: 246,blue: 246,alpha: 1)
        let leftBarButton = UIButton(frame:CGRect(x: 0,y: 0,width: 20,height: 20))
        leftBarButton.setBackgroundImage(UIImage(named: "fanhui"), for: UIControlState())
        leftBarButton.addTarget(self, action: #selector(backToPrevious), for: .touchUpInside)
        let leftBarButtonItem = UIBarButtonItem(customView: leftBarButton)
        self.navigationItem.leftBarButtonItems = [leftBarButtonItem]
        
        //设置右边的按钮
        let rightBarButton = UIButton(frame:CGRect(x: screenWidth-20,y: 0,width: 20,height: 20))
        rightBarButton.setBackgroundImage(UIImage(named: "chakanshoucang"), for: UIControlState())
        let rightBarButtonItem = UIBarButtonItem(customView: rightBarButton)
        rightBarButton.addTarget(self, action: #selector(chaKanShouCangTiMu), for: .touchUpInside)
        
        self.navigationItem.rightBarButtonItems = [rightBarButtonItem]
        
        
        //获取导航栏的高度
        navigationHeight = self.navigationController?.navigationBar.bounds.size.height
        
        //初始化题干label对象
        tiHaoLabel = UILabel(frame:CGRect(x: 10,y: navigationHeight!+30,width: 25,height: 30))
        fengZhuangLabelAttributes(tiHaoLabel!)
        
        //初始化题干内容label对象
        tiGanNeiRongLabel = UILabel(frame:CGRect(x: 30,y: navigationHeight!+30,width: 210,height: 30))
        fengZhuangLabelAttributes(tiGanNeiRongLabel!)
        
        //初始化随机生成数label对象
        randomNumberLable = UILabel(frame:CGRect(x: 240,y: navigationHeight!+30,width: 20,height: 30))
        fengZhuangLabelAttributes(randomNumberLable!)
        
        
        //初始化"的是"label对象
        diShiLabel = UILabel(frame:CGRect(x: 255,y: navigationHeight!+30,width: 40,height: 30))
        fengZhuangLabelAttributes(diShiLabel!)
        
        //初始化左括号label对象
        zuoKuoHaoLabel = UILabel(frame:CGRect(x: 293,y: navigationHeight!+30,width: 10,height: 30))
        fengZhuangLabelAttributes(zuoKuoHaoLabel!)
        
        //初始选中的选项Label对象
        selectedOptionLabel = UILabel(frame:CGRect(x: 300,y: navigationHeight!+30,width: 15,height: 30))
        fengZhuangLabelAttributes(selectedOptionLabel!)
        
        //初始化右括号label对象
        youKuoHaoLabel = UILabel(frame:CGRect(x: 315,y: navigationHeight!+30,width: 10,height: 30))
        fengZhuangLabelAttributes(youKuoHaoLabel!)
        
        //初始化四个imageView
        leftTopImageView = UIImageView(frame:CGRect(x:10,y:navigationHeight!+30+40,width:(screenWidth-40)/2,height:(screenWidth-40)/2))
        leftTopImageView!.contentMode = .scaleAspectFit
        
        rightTopImageView = UIImageView(frame:CGRect(x:screenWidth/2+10,y:navigationHeight!+70,width:(screenWidth-40)/2,height:(screenWidth-40)/2))
        rightTopImageView!.contentMode = .scaleAspectFit
        
        leftDownImageView = UIImageView(frame:CGRect(x:10,y:navigationHeight!+105+(screenWidth-40)/2,width:(screenWidth-40)/2,height:(screenWidth-40)/2))
        leftDownImageView!.contentMode = .scaleAspectFit
        
        rightDownImageView = UIImageView(frame:CGRect(x:screenWidth/2+10,y:navigationHeight!+105+(screenWidth-40)/2,width:(screenWidth-40)/2,height:(screenWidth-40)/2))
        rightDownImageView!.contentMode = .scaleAspectFit
        
        //初始化四个按钮
        buttonA = UIButton(frame:CGRect(x: 10+((screenWidth-40)/2-40)/2,y: navigationHeight!+70+(screenWidth-40)/2,width: 60,height: 20))
        fengZhuangButtonAttributes(buttonA!)
        
        buttonB = UIButton(frame:CGRect(x: screenWidth/2+10+((screenWidth-40)/2-40)/2,y: navigationHeight!+70+(screenWidth-40)/2,width: 60,height: 20))
        fengZhuangButtonAttributes(buttonB!)
        
        buttonC = UIButton(frame:CGRect(x: 10+((screenWidth-40)/2-40)/2,y: navigationHeight!+105+screenWidth-40,width: 60,height: 20))
        fengZhuangButtonAttributes(buttonC!)
        
        buttonD = UIButton(frame:CGRect(x: screenWidth/2+10+((screenWidth-40)/2-40)/2,y: navigationHeight!+105+screenWidth-40,width: 60,height: 20))
        fengZhuangButtonAttributes(buttonD!)
        
        //初始化保存按钮
        saveBtn = UIButton(frame:CGRect(x: 10,y: screenHeight-50,width: (screenWidth-40)/2,height: 40))
        fengZhuangButtonAttributes(saveBtn!)
        
        nextQuestionBtn = UIButton(frame:CGRect(x: screenWidth/2+10,y: screenHeight-50,width: (screenWidth-40)/2,height: 40))
        //初始化下一题按钮
        fengZhuangButtonAttributes(nextQuestionBtn!)

    }
    
    /*
     作者:张振东
     日期:2017-10-20
     功能:封装viewDidLoad
     */
    func fengZhuangViewDidLoad(){
        suiJiShengChengYiGeShu()
        fengZhuangKongJianShuXing()
        let biXingPath = Bundle.main.path(forResource: "bixingimage",ofType: nil, inDirectory: nil)
        print(biXingPath as Any)
        
        //从指定的目录下面读取文件
        do{
            try imageList = FileManager.default.contentsOfDirectory(atPath: biXingPath!)
            if imageList != nil{
                imageList?.remove(at: 0)
            }
        }
        catch let nsError as NSError{
            print(nsError)
        }
        
        shengChengTuPianBingFuZhi()
        
        buttonA?.addTarget(self, action: #selector(clickButtonA), for: .touchUpInside)
        buttonB?.addTarget(self, action: #selector(clickButtonB), for: .touchUpInside)
        buttonC?.addTarget(self, action: #selector(clickButtonC), for: .touchUpInside)
        buttonD?.addTarget(self, action: #selector(clickButtonD), for: .touchUpInside)
        
        saveBtn?.addTarget(self, action: #selector(clickSaveBtn), for: .touchUpInside)
        nextQuestionBtn?.addTarget(self, action: #selector(clickNextQuestion), for: .touchUpInside)
        self.view.addSubview(tiHaoLabel!)
        self.view.addSubview(tiGanNeiRongLabel!)
        self.view.addSubview(randomNumberLable!)
        self.view.addSubview(diShiLabel!)
        self.view.addSubview(zuoKuoHaoLabel!)
        self.view.addSubview(selectedOptionLabel!)
        self.view.addSubview(youKuoHaoLabel!)
        self.view.addSubview(leftTopImageView!)
        self.view.addSubview(rightTopImageView!)
        self.view.addSubview(leftDownImageView!)
        self.view.addSubview(rightDownImageView!)
        self.view.addSubview(buttonA!)
        self.view.addSubview(buttonB!)
        self.view.addSubview(buttonC!)
        self.view.addSubview(buttonD!)
        self.view.addSubview(saveBtn!)
        self.view.addSubview(nextQuestionBtn!)
    }

    /*
     作者:张振东
     时间:2016-10-20
     功能:实现图片的随机生成
     1、将可选的[String]数组强制类型转换
     2、获得该[String]数组的长度
     3、随记生成一个0-[String]数组的长度的整数
     4、通过第三步得到的数字可以获得该数组对应的值(也就是图片的名字)
     5、给图片imageView设置图片
     */
    func shengChengTuPianBingFuZhi(){
        if let cancelImageListOptional = imageList{
            print(cancelImageListOptional)
            for item in cancelImageListOptional{
                let headNumber = Int((item as NSString).substring(with: NSMakeRange(0, 1)))
                    if randomNumberValue == 0{
                        if headNumber == 0{
                            randomNumberArray.append("\(item)")
                        }else{
                            otherNumberArray.append("\(item)")
                        }
                    }else if randomNumberValue == 1{
                        if headNumber == 1{
                            randomNumberArray.append("\(item)")
                        }else{
                            otherNumberArray.append("\(item)")
                        }
                    }else if randomNumberValue == 2{
                        if headNumber == 2{
                            randomNumberArray.append("\(item)")
                        }else{
                            otherNumberArray.append("\(item)")
                        }
                    }else if randomNumberValue == 3{
                        if headNumber == 3{
                            randomNumberArray.append("\(item)")
                        }else{
                            otherNumberArray.append("\(item)")
                        }
                    }else if randomNumberValue == 4{
                        if headNumber == 4{
                            randomNumberArray.append("\(item)")
                        }else{
                            otherNumberArray.append("\(item)")
                        }
                    }else if randomNumberValue == 5{
                        if headNumber == 5{
                            randomNumberArray.append("\(item)")
                        }else{
                            otherNumberArray.append("\(item)")
                        }
                    }else if randomNumberValue == 6{
                        if headNumber == 6{
                            randomNumberArray.append("\(item)")
                        }else{
                            otherNumberArray.append("\(item)")
                        }
                    }else if randomNumberValue == 7{
                        if headNumber == 7{
                            randomNumberArray.append("\(item)")
                        }else{
                            otherNumberArray.append("\(item)")
                        }
                    }else if randomNumberValue == 8{
                        if headNumber == 8{
                            randomNumberArray.append("\(item)")
                        }else{
                            otherNumberArray.append("\(item)")
                        }
                    }else if randomNumberValue == 9{
                        if headNumber == 9{
                            randomNumberArray.append("\(item)")
                        }else{
                            otherNumberArray.append("\(item)")
                        }
                    }
            }
            
            /*
                这样执行完成之后便得到了两个数组:
                1、随机数所在的数组
                2、剩余的数组
                接下来要做的事情:
                   1、从随机数所在的数组中选中一个字符串,作为正确显示的图片对应的编码
                   2、从剩余的数组任意选择三个字符串,作为正确显示的图片对应的编码
                   3、随机生成一个数字,0-3,决定正确图片显示的位置
             */
            //获得随机数对应的数组的长度
            let randomNumberArrayLength = randomNumberArray.count
            print(randomNumberArrayLength)
            
            //获得剩余数组对应的数组的长度
            let otherNumberArrayLength = otherNumberArray.count
            print(otherNumberArrayLength)
            
//            let suiJiShuInRandomNumberArray:UInt32 = arc4random_uniform(UInt32(randomNumberArrayLength))
            
            let suiJiShuInRandomNumberArrays = createRandomMan(0,end: randomNumberArrayLength-1)
            let suiJiShuInRandomNumberArray = suiJiShuInRandomNumberArrays()
            
            //1、生成随机数
            let suiJiShuBetweenLingAndSan = createRandomMan(0,end: 3)
            print(suiJiShuBetweenLingAndSan())
            suiJiShuBetweenLingHeSan = suiJiShuBetweenLingAndSan()
            if suiJiShuBetweenLingHeSan == 0{
                leftTopImageName = randomNumberArray[Int(suiJiShuInRandomNumberArray!)]
                //给图片imageView设置图片
                let biXingImage = UIImage(named: leftTopImageName!)
                leftTopImageView?.image = biXingImage
                
                for indexs in 0...2{
                    let suiJiShuInOtherNumberArrayValue = createRandomMan(0,end: otherNumberArrayLength-1)
                    print(suiJiShuInOtherNumberArrayValue)
                    if indexs == 0{
                        rightTopImageName = otherNumberArray[Int(suiJiShuInOtherNumberArrayValue())]
                        let rightTopImage = UIImage(named: rightTopImageName!)
                        rightTopImageView?.image = rightTopImage
                    }else if indexs == 1{
                        leftDownImageName = otherNumberArray[Int(suiJiShuInOtherNumberArrayValue())]
                        let leftDownImage = UIImage(named: leftDownImageName!)
                        leftDownImageView?.image = leftDownImage

                    }else if indexs == 2{
                        rightDownImageName = otherNumberArray[Int(suiJiShuInOtherNumberArrayValue())]
                        let rightDownImage = UIImage(named: rightDownImageName!)
                        rightDownImageView?.image = rightDownImage
                    }
                }
           
            }else if suiJiShuBetweenLingHeSan == 1{
                rightTopImageName = randomNumberArray[Int(suiJiShuInRandomNumberArray!)]
                //给图片imageView设置图片
                let biXingImage = UIImage(named: rightTopImageName!)
                rightTopImageView?.image = biXingImage
                for indexs in 0...2{
                    let suiJiShuInOtherNumberArrayValue = createRandomMan(0,end: otherNumberArrayLength-1)
                    if indexs == 0{
                        leftTopImageName = otherNumberArray[Int(suiJiShuInOtherNumberArrayValue())]
                        let leftTopImage = UIImage(named: leftTopImageName!)
                        leftTopImageView?.image = leftTopImage
                    }else if indexs == 1{
                        leftDownImageName = otherNumberArray[Int(suiJiShuInOtherNumberArrayValue())]
                        let leftDownImage = UIImage(named: leftDownImageName!)
                        leftDownImageView?.image = leftDownImage
                        
                    }else if indexs == 2{
                        rightDownImageName = otherNumberArray[Int(suiJiShuInOtherNumberArrayValue())]
                        let rightDownImage = UIImage(named: rightDownImageName!)
                        rightDownImageView?.image = rightDownImage
                    }
                }
            }else if suiJiShuBetweenLingHeSan == 2{
                leftDownImageName = randomNumberArray[Int(suiJiShuInRandomNumberArray!)]
                //给图片imageView设置图片
                let biXingImage = UIImage(named: leftDownImageName!)
                leftDownImageView?.image = biXingImage
                for indexs in 0...2{
                    let suiJiShuInOtherNumberArrayValue = createRandomMan(0,end: otherNumberArrayLength-1)
                    if indexs == 0{
                        leftTopImageName = otherNumberArray[Int(suiJiShuInOtherNumberArrayValue())]
                        let leftTopImage = UIImage(named: leftTopImageName!)
                        leftTopImageView?.image = leftTopImage
                    }else if indexs == 1{
                        rightTopImageName = otherNumberArray[Int(suiJiShuInOtherNumberArrayValue())]
                        let rightTopImage = UIImage(named: rightTopImageName!)
                        rightTopImageView?.image = rightTopImage
                        
                    }else if indexs == 2{
                        rightDownImageName = otherNumberArray[Int(suiJiShuInOtherNumberArrayValue())]
                        let rightDownImage = UIImage(named: rightDownImageName!)
                        rightDownImageView?.image = rightDownImage
                    }
                }

            }else if suiJiShuBetweenLingHeSan == 3{
                rightDownImageName = randomNumberArray[Int(suiJiShuInRandomNumberArray!)]
                //给图片imageView设置图片
                let biXingImage = UIImage(named: rightDownImageName!)
                rightDownImageView?.image = biXingImage
                for indexs in 0...2{
                    let suiJiShuInOtherNumberArrayValue = createRandomMan(0,end: otherNumberArrayLength-1)
                    if indexs == 0{
                        leftTopImageName = otherNumberArray[Int(suiJiShuInOtherNumberArrayValue())]
                        let leftTopImage = UIImage(named: leftTopImageName!)
                        leftTopImageView?.image = leftTopImage
                    }else if indexs == 1{
                        rightTopImageName = otherNumberArray[Int(suiJiShuInOtherNumberArrayValue())]
                        let rightTopImage = UIImage(named: rightTopImageName!)
                        rightTopImageView?.image = rightTopImage
                        
                    }else if indexs == 2{
                        leftDownImageName = otherNumberArray[Int(suiJiShuInOtherNumberArrayValue())]
                        let leftDownImage = UIImage(named: leftDownImageName!)
                        leftDownImageView?.image = leftDownImage
                    }
                }
            }
        }
    }

    
    /*
     作者:张振东
     时间:2016-10-21
     功能:随机数生成器函数
     */
    func createRandomMan(_ start: Int, end: Int) ->() ->Int! {
        //根据参数初始化可选值数组
        var nums = [Int]();
        for i in start...end{
            nums.append(i)
        }
        
        func randomMan() -> Int! {
            if !nums.isEmpty {
                //随机返回一个数，同时从数组里删除
                let index = Int(arc4random_uniform(UInt32(nums.count)))
                return nums.remove(at: index)
            }
            else {
                //所有值都随机完则返回nil
                return nil
            }
        }
        return randomMan
    }
    
    /*
     作者:张振东
     时间:2016-10-21
     功能:按钮A被选中
         按钮A被选中需要做的一下几件事情:
         1、判断是否为正确答案,如果答案正确,则按钮的颜色变成灰色,小括号中的值则跟着发生变化
            如果答案错误,则按钮的颜色变成红色,小括号中的值也跟着发生变化
     */
    func clickButtonA(){
        //表示选中的为正确答案
        if !(buttonA?.isSelected)! == true{
            if suiJiShuBetweenLingHeSan == 0{
                buttonA?.backgroundColor = UIColor.green
                selectedOptionLabel!.text = "A"
            }else{
                buttonA?.backgroundColor = UIColor.gray
                selectedOptionLabel!.text = "A"
            }

        }
    }
    
    func clickButtonB(){
        //表示选中的为正确答案
        if !(buttonB?.isSelected)! == true{
            if suiJiShuBetweenLingHeSan == 1{
                buttonB?.backgroundColor = UIColor.green
                selectedOptionLabel!.text = "B"
            }else{
                buttonB?.backgroundColor = UIColor.gray
                selectedOptionLabel!.text = "B"
            }
            
        }
        print(buttonA?.isSelected as Any)

    }
    
    func clickButtonC(){
        //表示选中的为正确答案
        if !(buttonC?.isSelected)! == true{
            if suiJiShuBetweenLingHeSan == 2{
                buttonC?.backgroundColor = UIColor.green
                selectedOptionLabel!.text = "C"
            }else{
                buttonC?.backgroundColor = UIColor.gray
                selectedOptionLabel!.text = "C"
            }
            
        }

    }
    
    func clickButtonD(){
        //表示选中的为正确答案
        if !(buttonD?.isSelected)! == true{
            if suiJiShuBetweenLingHeSan == 3{
                buttonD?.backgroundColor = UIColor.green
                selectedOptionLabel!.text = "D"
            }else{
                buttonD?.backgroundColor = UIColor.gray
                selectedOptionLabel!.text = "D"
            }
        }
    }
    
    
    /*
     作者:张振东
     时间:2016-10-21
     功能:下一个按钮被点击
     点击下一个按钮需要做的以下几件事情：
     1、题号会自动加一
     2、随机数自动生成
     3、题干内容重新加载,不发生变化
     4、随机生成其他图片
     */
    func clickNextQuestion(){
        //1、题号会自动加一
        tiHao = tiHao + 1
        //设置热身题为50道
        if tiHao <= 20{
            self.tiHaoLabel?.text = "\(tiHao)"
            
            //2、随机数自动生成,并将上次生成的随机数删除,将选项的内容置空,将题号置空
            self.tiHaoLabel?.text = ""
            self.randomNumberLable?.text = ""
            self.selectedOptionLabel?.text = ""
            self.tiGanNeiRongLabel?.text = ""
            self.diShiLabel?.text = ""
            self.zuoKuoHaoLabel?.text = ""
            self.youKuoHaoLabel?.text = ""
            
            //将两个数组置为空,否则后期会出现叠加的问题
            randomNumberArray = []
            otherNumberArray = []
            //3、题干内容重新加载,不发生变化
            fengZhuangViewDidLoad()
        }else{
            //让下一个按钮失去交互性
            nextQuestionBtn?.isUserInteractionEnabled = false
            //热身结束了
            let alertController = UIAlertController(title: "温馨提示",
                                                    message: "亲,热身结束了!!", preferredStyle: .alert)
            let okAction = UIAlertAction(title: "好的", style: .default, handler: {
                action in
                print("点击了确定")
            })
            alertController.addAction(okAction)
            self.present(alertController, animated: true, completion: nil)
        }
        
    }
    
    /*
     作者:张振东
     时间:2016-10-21
     功能:保存按钮被点击
    */
    func clickSaveBtn(){
        //将需要保存的属性封装为类对象进行存储
        let biXingDanXuanTi = BiXingDanXuanTi()

        biXingDanXuanTi.tiHao = self.tiHaoLabel?.text
        print(biXingDanXuanTi.tiHao as Any)
        biXingDanXuanTi.randomNumber = self.randomNumberLable?.text
        print(biXingDanXuanTi.randomNumber as Any)
        biXingDanXuanTi.selectedOptionValue = self.selectedOptionLabel?.text
        print(biXingDanXuanTi.selectedOptionValue as Any)
        biXingDanXuanTi.leftTopImageName = leftTopImageName
        print(biXingDanXuanTi.leftTopImageName as Any)
        biXingDanXuanTi.rightTopImageName = self.rightTopImageName
        print(biXingDanXuanTi.rightTopImageName as Any)
        biXingDanXuanTi.leftDownImageName = self.leftDownImageName
        print(biXingDanXuanTi.leftDownImageName as Any)
        biXingDanXuanTi.rightDownImageName = self.rightDownImageName
        print(biXingDanXuanTi.rightDownImageName as Any)
        
        //使用默认的数据库
        let realm = try! Realm()
        // 使用默认的目录，但是使用用户名来替换默认的文件名
        //查询所有的消费记录
//        let items = realm.objects(BiXingDanXuanTi)
        //已经有记录的话就不插入了
//        if items.count > 0 {
//            return
//        }
        // 数据持久化操作（类型记录也会自动添加的）
        
        try! realm.write {
//            realm.deleteAll()
            realm.add(biXingDanXuanTi)
        }
    
        //打印出数据库地址
        print(realm.configuration.fileURL as Any)
        
        //弹出一个提示框,显示没有收藏题了
        let alertController = UIAlertController(title: "温馨提示",
                                                message: "亲,收藏成功了!!", preferredStyle: .alert)
        let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
        let okAction = UIAlertAction(title: "好的", style: .default, handler: {
            action in
            print("点击了确定")
        })
        alertController.addAction(cancelAction)
        alertController.addAction(okAction)
        self.present(alertController, animated: true, completion: nil)
    }
    
    func chaKanShouCangTiMu(){
        
        let chaKanBiXingShouCangDanXuanTiViewController = ChaKanBiXingShouCangDanXuanTiViewController()
        self.navigationController?.pushViewController(chaKanBiXingShouCangDanXuanTiViewController, animated: true)
    }
    
    
    func backToPrevious(){
        self.navigationController?.popViewController(animated: true)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
